package org.nanhill.commons.model.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * NTS
 * 
 * Abstract tree node.
 * 
 * @version 2007-4-5
 * @author xalinx at gmail dot com
 * 
 */
public abstract class AbstractTreeNode<E extends TreeNode> implements WritableTreeNode<E> {

	/**
	 * Parent.
	 */
	private E parent;

	public E getParent() {
		return parent;
	}

	public void setParent(E parent) {
		this.parent = parent;
	}

	/**
	 * Child nodes.
	 */
	private List<E> children;

	public List<E> getChildren() {
		return children;
	}

	/**
	 * Add one child.
	 * 
	 * @param child
	 */
	public void addChild(E child) {
		if (null == children) {
			children = new ArrayList<E>();
		}
		children.add(child);
	}

	public boolean isLeaf() {
		return (!isRoot()) && (null == children || children.isEmpty());
	}

	public boolean isRoot() {
		return null == parent;
	}

}
